package com.lm56.web.sys.controller;

import com.lm56.common.Utils;
import com.lm56.web.network.domain.Location;
import com.lm56.web.sys.domain.UserForm;
import com.lm56.web.sys.domain.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * Created by Kelvin Yeung on 2016/11/5.
 */
public class RegisterValidator implements Validator {
    public boolean supports(Class<?> aClass) {
        return UserForm.class.equals(aClass);
    }

    public void validate(Object obj, Errors errors) {
        UserForm user = (UserForm) obj;
        //手机号码验证
        String phoneNo = user.getPhoneNo();
        if(StringUtils.isBlank(phoneNo)){
            errors.rejectValue("phoneNo",null,"手机号码不能为空");
        }else if(!Utils.isMobilePhoneNo(phoneNo)){
            errors.rejectValue("phoneNo",null,"请输入正确的手机号码");
        }
        //密码验证
        String pwd = user.getPassword();
        String pwd2 = user.getPassword();
        if(StringUtils.isBlank(pwd)){
            errors.rejectValue("password",null,"请输入密码");
        }else if(!Utils.isValidPassword(pwd)){
            errors.rejectValue("password",null,"请输入正确的手机号码");
        }

        if(StringUtils.isBlank(pwd2)||!pwd2.equals(pwd)){
            errors.rejectValue("password2",null,"两次输入的密码必须一致");
        }


        String email = user.getEmail();
        if(StringUtils.isBlank(email)){
            errors.rejectValue("email",null,"请输入电子邮箱地址");

        }else if(!Utils.isEmail(email)){
            errors.rejectValue("email",null,"请输入正确的电子邮箱地址");

        }


        int userType = user.getUserType();
        if(userType== User.USER_TYPE_COMPANY){

            Location location =  user.getCompanyLocation();
            if(location ==null){
                errors.rejectValue("companyLocation",null,"请选择公司所在地");
            } else if(location.getLocationId()==null || location.getLocationId() <=0){
                errors.rejectValue("companyLocation",null,"请选择公司所在地");
            }


            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "companyName", null, "请输入公司名称");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors,"companyContactName",null,"请输入公司联系人名称");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "companyShippingTypes", null, "请输入运输方式");
        }else if(userType== User.USER_TYPE_SENDER){

            Location location =  user.getSenderLocation();
            if(location.getLocationId()==null || location.getLocationId() <=0){
                errors.rejectValue("senderLocation",null,"请选择所在地");
            }

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,"senderName",null,"请输入你的姓名");
        }else{

            Location location =  user.getOtherLocation();
            if(location.getLocationId()==null || location.getLocationId() <=0){
                errors.rejectValue("otherLocation",null,"请输入所在地");
            }
            ValidationUtils.rejectIfEmptyOrWhitespace(errors,"otherName",null,"请输入你的姓名");
        }


    }
}
